package com.xxd.common.validator.group;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @Description: 手写校验器ConstraintValidator<注解类型，目标类型></>
 * @Author: xuxd
 * @Date: 2022/10/11 16:40
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    private Set<Integer> set = new HashSet<>();
    //初始化
    @Override
    public void initialize(ListValue constraintAnnotation) {
        if (constraintAnnotation !=null){
        int[] values = constraintAnnotation.values();
            for (int i : values) {
                set.add(i);
            }
        }

    }
    //是否校验成功
    /**
     * @Author: xuxd
     * @param :value需要校验的值
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(value);
    }
}
